package com.juku2024.juku.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import com.juku2024.juku.R
import android.animation.ObjectAnimator
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.widget.Button
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.juku2024.juku.adapter.EpisodeAdapter
import com.juku2024.juku.model.EpisodesModel
import com.juku2024.juku.tools.AppUtils
import com.juku2024.juku.tools.Constants
import com.juku2024.juku.tools.WrapGridLayoutManager
import com.juku2024.juku.callback.OnEpisodeClickListener


class VideoEpisodeView(context: Context, attributeSet: AttributeSet) : ConstraintLayout(context, attributeSet) {

    private var isEpisodeReverse = false
    var episodeClick: OnEpisodeClickListener? = null
    private val episodeListAdapter by lazy { EpisodeAdapter(ArrayList()) }
    
    private val btnBack: Button
    private val episodeRecycler: RecyclerView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.view_video_episode, this, true)
        val color = AppUtils.getColor(R.color.app_window_background)
        view.setBackgroundColor(color)

        btnBack = view.findViewById(R.id.btnBack)
        episodeRecycler = view.findViewById(R.id.episodeRecycler)

        btnBack.setOnClickListener {
            hide()
        }
        episodeRecycler.setHasFixedSize(true)
        episodeRecycler.adapter = episodeListAdapter
        episodeListAdapter.setOnItemClickListener { _, _, position ->
            if (episodeListAdapter.data[position].isPlaying) return@setOnItemClickListener


            // 更新item
            episodeListAdapter.data.forEachIndexed { index, episodesModel ->
                episodesModel.isPlaying = index == position
            }
            episodeListAdapter.notifyDataSetChanged()
            val target = if (isEpisodeReverse) {
                episodeListAdapter.itemCount - position - 1
            } else {
                position
            }
            episodeClick?.onEpisodeClick(target, false)
            hide()
        }
    }

    fun setEpisodes(lockEps: Int, isLockAd: Boolean, category: Int, episodes: ArrayList<EpisodesModel>) {
        episodeListAdapter.lockEps = lockEps
        episodeListAdapter.isLockAd = isLockAd
        episodeListAdapter.category = category
        episodeRecycler.layoutManager = if (category == Constants.VIDEO_CATEGORY_VARIFY || category == Constants.VIDEO_CATEGORY_MOVIE) {
            WrapGridLayoutManager(context, 2)
        } else {
            WrapGridLayoutManager(context, 6)
        }
        episodeListAdapter.setList(episodes)
    }

    fun setEpisodes(isEpisodeReverse: Boolean, adapter: EpisodeAdapter) {
        this.isEpisodeReverse = isEpisodeReverse
        episodeListAdapter.lockEps = adapter.lockEps
        episodeListAdapter.isLockAd = adapter.isLockAd
        episodeListAdapter.category = adapter.category
        episodeRecycler.layoutManager = if (adapter.category == Constants.VIDEO_CATEGORY_VARIFY || adapter.category == Constants.VIDEO_CATEGORY_MOVIE) {
            WrapGridLayoutManager(context, 2)
        } else {
            WrapGridLayoutManager(context, 6)
        }
        episodeListAdapter.setList(adapter.data)
    }

    private var isShowingDes = false
    fun show(episode: Int) {
        episodeListAdapter.data.forEachIndexed { index, episodesModel ->
            episodesModel.isPlaying = index == episode
        }
        episodeListAdapter.notifyDataSetChanged()
        isShowingDes = true
        val animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "translationY", 2000f, 0f)
        animator.duration = 200
        animator.interpolator = DecelerateInterpolator()
        animator.start()
    }

    fun isShowing(): Boolean {
        return isShowingDes
    }

    fun hide() {
        isShowingDes = false
        val animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "translationY", 0f, 2000f)
        animator.duration = 200
        animator.interpolator = AccelerateInterpolator()
        animator.start()
    }

}